package edu.gatech.asthma.activity;

import edu.gatech.asthma.R;
import edu.gatech.asthma.database.DBAdapter;
import edu.gatech.asthma.ui_support.HelpMenuHelper;
import edu.gatech.asthma.ui_support.ParentMenu;
import edu.gatech.asthma.ui_support.ProfileCustomizer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

/**
 * Home Screen that displays all user options
 */
public class HomeScreenActivity extends Activity {

	int identifier = -1;
	DBAdapter database;
	ProfileCustomizer customizer;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home_screen);
		
		getActionBar().setTitle("Home");

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			identifier = extras.getInt("ID");
		}
		database = new DBAdapter(this);
		database.open();
		customizer = new ProfileCustomizer(this,this.findViewById(android.R.id.content),database,identifier);
		customizer.setCustomizations();

		//BUTTON LISTENERS
		final LinearLayout test = (LinearLayout) findViewById(R.id.home_button_test);
		test.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(HomeScreenActivity.this, SubjectiveTestActivity.class);
				i.putExtra("ID", identifier);
				HomeScreenActivity.this.startActivity(i);
			}});

		final LinearLayout symptoms = (LinearLayout) findViewById(R.id.home_button_symptoms);
		symptoms.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(HomeScreenActivity.this, SymptomActivity.class);
				i.putExtra("ID", identifier);
				HomeScreenActivity.this.startActivity(i);
			}});

		final LinearLayout library = (LinearLayout) findViewById(R.id.home_button_library);
		library.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(HomeScreenActivity.this, SymptomActivity.class);
				i.putExtra("ID", identifier);
				HomeScreenActivity.this.startActivity(i);
			}});

		final LinearLayout schedule = (LinearLayout) findViewById(R.id.home_button_calendar);
		schedule.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(HomeScreenActivity.this, ScheduleActivity.class);
				i.putExtra("ID", identifier);
				HomeScreenActivity.this.startActivity(i);
			}});

		final LinearLayout game = (LinearLayout) findViewById(R.id.home_button_games);
		game.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(HomeScreenActivity.this, SymptomActivity.class);
				i.putExtra("ID", identifier);
				HomeScreenActivity.this.startActivity(i);
			}});

		final LinearLayout profile = (LinearLayout) findViewById(R.id.home_button_profile);
		profile.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(HomeScreenActivity.this, EditProfileChildActivity.class);
				i.putExtra("ID", identifier);
				HomeScreenActivity.this.startActivity(i);
			}});

		final LinearLayout help = (LinearLayout) findViewById(R.id.home_button_help);
		help.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(HomeScreenActivity.this, ShowContactsActivity.class);
				i.putExtra("ID", identifier);
				HomeScreenActivity.this.startActivity(i);
			}});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return ParentMenu.setUpParentMenu(menu, this);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i;
		switch (item.getItemId()) {
		case R.id.parent_menu_contacts:
			i = new Intent(HomeScreenActivity.this, ShowContactsActivity.class);
			i.putExtra("ID", identifier);
			HomeScreenActivity.this.startActivity(i);
			return true;
		case R.id.parent_menu_info:
			i = new Intent(HomeScreenActivity.this, ShowProfileActivity.class);
			i.putExtra("ID", identifier);
			HomeScreenActivity.this.startActivity(i);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
	/**
	 * User clicked the help button on this screen
	 * @param v ImageView clicked
	 */
	public void helpButtonClicked(View v) {
		HelpMenuHelper.displayHelpMessage(this);
	}
}